/**
 * 产品生产BOM
 */
Ext.define("YX.smat.ManufactureBillOfMaterial", {
  extend: "Ext.panel.Panel",
  gridBom: null,
  btnAddAltBom: null,
  btnRemoveAltBom: null,
  gridAltBom: null,
  pelBom: null,
  txtSearchProdCode: null,
  btnAdd: null,
  btnRemove: null,
  btnSave: null,
  btnAlternate: null,
  btnSearch: null,
  frmSearch: null,
  layout: {
    type: "hbox",
    align: "stretch",
  },
  initComponent: function () {
    var bomStore = Ext.create("Ext.data.JsonStore", {
      fields: [
        "prodCode",
        "prodVer",
        "matCode",
        "matVer",
        "matDesc",
        "altFlag",
        {
          type: "number",
          name: "qty",
        },
        "unit",
        "lossRate",
        {
          type: "number",
          name: "lossQty",
        },
        "memo",
      ],
      proxy: {
        type: "zbusrpc",
        module: "SmatViewProductManufactureMaterialService",
        reader: {
          root: "bomList",
        },
      },
      listeners: {
        scope: this,
        load: this.onBomStoreLoad,
      },
    });

    this.btnAdd = Ext.create("Ext.button.Button", {
      text: T.label.add,
      iconCls: "icon-add",
      scope: this,
      handler: function () {
        this.gridBom.getStore().insert(0, {});
      },
    });

    this.btnRemove = Ext.create("Ext.button.Button", {
      text: T.label.remove,
      iconCls: "icon-remove",
      scope: this,
      handler: function () {
        var rcds = this.gridBom.getSelectionModel().getSelection();
        if (!Ext.isEmpty(rcds)) {
          this.gridBom.getStore().remove(rcds);
        }
      },
    });

    this.btnSave = Ext.create("Ext.button.Button", {
      text: T.label.save,
      itemId: "btnSave",
      iconCls: "icon-save",
      scope: this,
      handler: this.onBtnSaveClicked,
    });

    this.gridBom = Ext.create("Ext.grid.Panel", {
      title: T.label.manufactureBillOfMaterial,
      flex: 1,
      columns: [
        {
          text: T.label.prodCode,
          dataIndex: "prodCode",
          editor: {
            xtype: "codeviewfieldx",
            dspFields: [
              {
                dspFieldId: "prodDesc",
                dspDataIndex: "matDesc",
              },
              {
                dspFieldId: "prodVer",
                dspDataIndex: "matVer",
              },
            ],
            columns: [
              {
                xtype: "gridcolumn",
                disabledSearch: false,
                hidden: false,
                dataIndex: "matCode",
                text: T.label.matCode,
              },
              {
                xtype: "gridcolumn",
                disabledSearch: false,
                hidden: false,
                dataIndex: "matDesc",
                text: T.label.matDesc,
              },
              {
                xtype: "gridcolumn",
                disabledSearch: true,
                hidden: false,
                dataIndex: "matVer",
                text: T.label.matVer,
              },
            ],
            editable: false,
            store: Ext.create("Ext.data.JsonStore", {
              fields: ["matCode", "matVer", "matDesc"],
              proxy: {
                type: "zbusrpc",
                module: "SbasViewProductService",
                reader: {
                  type: "json",
                  root: "productList",
                },
              },
            }),
          },
        },
        {
          text: T.label.prodVer,
          dataIndex: "prodVer",
          width: 80,
        },
        {
          text: T.label.prodDesc,
          dataIndex: "prodDesc",
          width: 200,
        },
        {
          text: T.label.matCode,
          dataIndex: "matCode",
          editor: {
            xtype: "codeviewfieldx",
            dspFields: [
              {
                dspFieldId: "matDesc",
                dspDataIndex: "matDesc",
              },
              {
                dspFieldId: "matVer",
                dspDataIndex: "matVer",
              },
              {
                dspFieldId: "matUnit",
                dspDataIndex: "matUnit",
              },
            ],
            columns: [
              {
                xtype: "gridcolumn",
                disabledSearch: false,
                hidden: false,
                dataIndex: "matCode",
                text: T.label.matCode,
              },
              {
                xtype: "gridcolumn",
                disabledSearch: false,
                hidden: false,
                dataIndex: "matDesc",
                text: T.label.matDesc,
              },
              {
                xtype: "gridcolumn",
                disabledSearch: true,
                hidden: false,
                dataIndex: "matVer",
                text: T.label.matVer,
              },
            ],
            editable: false,
            store: Ext.create("Ext.data.JsonStore", {
              fields: ["matCode", "matVer", "matDesc", "matUnit"],
              proxy: {
                type: "zbusrpc",
                module: "SbasViewBaseMaterialService",
                reader: {
                  type: "json",
                  root: "matList",
                },
              },
            }),
          },
        },
        {
          text: T.label.matVer,
          dataIndex: "matVer",
          width: 80,
        },
        {
          text: T.label.matDesc,
          dataIndex: "matDesc",
          width: 150,
        },
        {
          text: T.label.qty,
          dataIndex: "qty",
          width: 60,
          editor: {
            xtype: "numberfield",
            minValue: 0,
            allowDecimals: true,
            decimalPrecision: 4,
          },
        },
        {
          text: T.label.unit,
          dataIndex: "matUnit",
          width: 60,
        },
        {
          text: T.label.lossRate,
          dataIndex: "lossRate",
          width: 70,
          editor: {
            xtype: "numberfield",
            minValue: 0,
            allowDecimals: true,
            decimalPrecision: 4,
          },
        },
        {
          text: T.label.lossQty,
          dataIndex: "lossQty",
          width: 70,
          editor: {
            xtype: "numberfield",
            minValue: 0,
            allowDecimals: true,
            decimalPrecision: 4,
          },
        },
        {
          text: T.label.memo,
          dataIndex: "memo",
          editor: {
            xtype: "textfield",
          },
        },
      ],
      plugins: [
        Ext.create("Ext.grid.plugin.RowEditing", {
          clicksToEdit: 2,
        }),
      ],
      store: bomStore,
      tbar: [this.btnAdd, this.btnRemove, "-", this.btnSave],
      bbar: {
        xtype: "pagingtoolbar",
        store: bomStore,
        displayInfo: true,
      },
    });

    this.btnAddAltBom = Ext.create("Ext.button.Button", {
      iconCls: "icon-add",
      scope: this,
      handler: this.onBtnAddAltBomClicked,
    });
    this.btnRemoveAltBom = Ext.create("Ext.button.Button", {
      iconCls: "icon-remove",
      scope: this,
      handler: this.onBtnRemoveAltBomClicked,
    });

    this.gridAltBom = Ext.create("Ext.grid.Panel", {
      title: T.label.manufactureBillOfAlternateMaterial,
      flex: 1,
      tbar: {
        items: [this.btnAddAltBom, "-", this.btnRemoveAltBom],
      },
      plugins: [
        Ext.create("Ext.grid.plugin.RowEditing", {
          clicksToEdit: 1,
        }),
      ],
      columns: [
        {
          width: 20,
          renderer: function (value, metaData, record) {
            metaData.tdCls =
              record.get("delFlag") == "Y" ? "icon-remove" : "icon-add";
          },
        },
        {
          text: T.label.matCode,
          dataIndex: "matCode",
          itemId: "colAltMatCode",
          editor: {
            xtype: "codeviewfieldx",
            columns: [
              {
                xtype: "gridcolumn",
                disabledSearch: false,
                hidden: false,
                dataIndex: "matCode",
                text: T.label.matCode,
              },
              {
                text: T.label.matVer,
                dataIndex: "matVer",
              },
              {
                text: T.label.matDesc,
                disabledSearch: true,
                dataIndex: "matDesc",
                flex: 1,
              },
            ],
            dspFields: [
              {
                dspFieldId: "matVer",
                dspDataIndex: "matVer",
              },
              {
                dspFieldId: "matDesc",
                dspDataIndex: "matDesc",
              },
              {
                dspFieldId: "matUnit",
                dspDataIndex: "matUnit",
              },
            ],
            store: {
              store: "json",
              fields: ["matCode", "matVer", "matDesc", "matUnit"],
              proxy: {
                type: "zbusrpc",
                module: "SbasViewBaseMaterialService",
                reader: {
                  root: "matList",
                },
              },
            },
          },
        },
        {
          text: T.label.matVer,
          dataIndex: "matVer",
          width: 80,
        },
        {
          text: T.label.matDesc,
          dataIndex: "matDesc",
          width: 300,
        },
        {
          text: T.label.altMatCode,
          dataIndex: "mainMatCode",
        },
        {
          text: T.label.priority,
          dataIndex: "priority",
          width: 70,
          editor: {
            xtype: "combobox",
            editable: false,
            valueField: "code",
            displayField: "desc",
            store: {
              xtype: "store",
              fields: ["code", "desc"],
              data: [
                ["L", T.label.low],
                ["M", T.label.Medium],
                ["H", T.label.High],
              ],
            },
          },
        },
        {
          text: T.label.qty,
          width: 60,
          dataIndex: "qty",
          editor: {
            xtype: "numberfield",
            minValue: 0,
            allowDecimals: true,
            decimalPrecision: 4,
          },
        },
        {
          text: T.label.unit,
          dataIndex: "matUnit",
          width: 60,
        },
        {
          text: T.label.lossRate,
          dataIndex: "lossRate",
          width: 70,
          editor: {
            xtype: "numberfield",
            minValue: 0,
            allowDecimals: true,
            decimalPrecision: 4,
          },
        },
        {
          text: T.label.lossQty,
          width: 70,
          dataIndex: "lossQty",
          editor: {
            xtype: "numberfield",
            minValue: 0,
            allowDecimals: true,
            decimalPrecision: 4,
          },
        },
        {
          text: T.label.memo,
          dataIndex: "memo",
          editor: {
            xtype: "textfield",
            maxLength: 100,
          },
        },
      ],
      store: Ext.create("Ext.data.JsonStore", {
        pageSize: 100,
        fields: [
          "prodCode",
          "prodVer",
          "matCode",
          "matVer",
          "matDesc",
          "mainMatCode",
          "priority",
          {
            type: "number",
            name: "qty",
          },
          "matUnit",
          "lossRate",
          {
            type: "number",
            name: "lossQty",
          },
          "memo",
        ],
        proxy: {
          type: "zbusrpc",
          module: "SmatViewProductManufactureAlternateMaterialService",
          reader: {
            root: "altList",
          },
        },
      }),
    });

    this.pelBom = Ext.create("Ext.panel.Panel", {
      flex: 1,
      layout: {
        type: "vbox",
        align: "stretch",
      },
      items: [this.gridBom, this.gridAltBom],
    });

    this.txtSearchProdCode = Ext.create("Ext.form.field.Text", {
      fieldLabel: T.label.prodCode,
      labelWidth: 60,
      anchor: "100%",
      scope: this,
      listeners: {
        specialkey: function (field, e) {
          if (e.getKey() == e.ENTER) {
            this.onBtnSearchClicked();
          }
        },
        scope: this,
      },
    });

    this.btnAlternate = Ext.create("Ext.button.Button", {
      text: T.label.alternated,
      iconCls: "icon-save",
      itemId: "btnAlternate",
      scope: this,
      handler: this.onBtnAlternateClicked,
    });
    this.btnSearch = Ext.create("Ext.button.Button", {
      text: T.label.search,
      iconCls: "icon-search",
      scope: this,
      handler: this.onBtnSearchClicked,
    });

    this.frmSearch = Ext.create("Ext.form.Panel", {
      title: T.label.search,
      width: 250,
      items: [this.txtSearchProdCode],
      bbar: {
        items: [this.btnAlternate, this.btnSearch],
      },
    });

    //this.gridAltBom.down('#colAltMatCode').getEditor().on('select', this.onColAltMatCodeSelect, this);

    this.items = [this.pelBom, this.frmSearch];

    this.callParent();
  },
  onBtnAddAltBomClicked: function (btn) {
    var index = this.gridAltBom.getStore().findExact("matCode", "");
    if (index >= 0) {
      this.gridAltBom.getSelectionModel().select(index);
    } else {
      var mRcds = this.gridBom.getSelectionModel().getSelection();
      if (!Ext.isEmpty(mRcds)) {
        this.gridAltBom.getStore().insert(0, {
          delFlag: "N",
          prodCode: mRcds[0].get("prodCode"),
          prodVer: mRcds[0].get("prodVer"),
          mainMatCode: mRcds[0].get("matCode"),
        });
      } else {
        this.gridAltBom.getStore().insert(0, {
          delFlag: "N",
        });
      }
    }
  },
  onBtnSaveClicked: function (btn) {
    var vBomList = [];
    var errRow = -1;
    this.gridBom.getStore().each(function (rcd, idx) {
      if (Ext.isEmpty(rcd.get("prodCode"))) {
        errRow = idx;
        return false;
      }
      if (Ext.isEmpty(rcd.get("matCode"))) {
        errRow = idx;
        return false;
      }
      if (Ext.isEmpty(rcd.get("qty"))) {
        errRow = idx;
        return false;
      }

      var d = Ext.clone(rcd.data);
      d.unit = d.matUnit;
      vBomList.push(d);
    });

    if (errRow >= 0) {
      Ext.MessageBox.show({
        title: T.lable.save,
        msg: "存在错误行，在" + (errRow + 1),
        icon: Ext.MessageBox.ERROR,
        buttons: Ext.MessageBox.OK,
      });
      return;
    }

    var me = this;
    Ext.Msg.show({
      title: T.label.save,
      msg: T.msg.are_you_sure_to_save_data,
      icon: Ext.Msg.QUESTION,
      buttons: Ext.Msg.YESNO,
      fn: function (btnId) {
        if (btnId == "yes") {
          btn.disable();

          YX.ZbusRpc.request({
            module: "SmatSaveProductManufactureMaterialService",
            params: {
              bomList: vBomList,
            },
            showSuccess: true,
            callback: function () {
              btn.enable();
            },
            success: function () {
              me.gridBom.getStore().commitChanges();
            },
          });
        }
      },
    });
  },
  onBtnRemoveAltBomClicked: function (btn) {
    var rcds = this.gridAltBom.getSelectionModel().getSelection();
    if (!Ext.isEmpt(rcds)) {
      this.gridAltBom.remove(rcds);
    }
  },
  onBtnSearchClicked: function (btn) {
    this.gridBom
      .getStore()
      .getProxy()
      .setExtraParam("prodCode", this.txtSearchProdCode.getValue());
    this.gridBom.getStore().loadPage(1);
  },
  onBtnAlternateClicked: function (btn) {
    var attList = [];
    this.gridAltBom.getStore().each(function (rcd) {
      if (rcd.get("matCode") != "" && rcd.get("delFlag") != "Y") {
        attList.push(rcd.data);
      }
    });

    if (attList.lengh == 0) {
      return;
    }

    var me = this;
    Ext.Msg.show({
      title: T.label.save,
      msg: T.msg.are_you_sure_to_save_data,
      icon: Ext.Msg.QUESTION,
      buttons: Ext.Msg.YESNO,
      fn: function (btnId) {
        if (btnId == "yes") {
          btn.disable();

          YX.ZbusRpc.request({
            module: "SmatSaveProductionManufactureAlternateMaterialService",
            params: {
              attList: attList,
            },
            showSuccess: true,
            callback: function () {
              btn.enable();
            },
            success: function () {
              me.gridAttBom.getStore().reload();
            },
          });
        }
      },
    });
  },
  onBomStoreLoad: function () {
    this.gridAltBom.getStore().removeAll();
  },
});
